package cn.biplam.front.template.control;

import java.util.ArrayList;

import cn.biplam.common.paramters.StyleConst;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Clear_Type;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.template.base.HiddenParam;
import cn.biplam.front.template.base.Text;

/**
 * 提示界面模版
 * <ul>用法
 * <li> new Confirm("提示信息","记录ID")  创建界面模版
 * <li> this.bind(,"提交动作ID")         绑定提交动作
 * <li> this.get()                     获取界面
 * </ul>
 * @author XiaoRuxing 2009-6-23下午03:03:46
 * @version 1.0
 */
public class Confirm {

	/** 提示窗口容器*/
	private HtmlDiv div;
	/** 提示信息 */
	private String sAlertTitle;
	/** 记录ID */
	private String recordId;
	/** 调用删除功能者页面ID */
	private String pId;
	
	private ArrayList<HtmlWindow> arTexts;
	
	/**
	 * 创建提示界面
	 * @param sAlertTitle 提示信息内容
	 * @param recordId    记录ID
	 * @throws ErrorMessage
	 */
	public Confirm(String sAlertTitle,String recordId) throws ErrorMessage{
		this(sAlertTitle,recordId,null);
	}
	
	/**
	 * 创建提示界面
	 * @param sAlertTitle 提示信息内容
	 * @param recordId    记录ID
	 * @param pId         调用者页面ID
	 * @throws ErrorMessage
	 */
	public Confirm(String sAlertTitle,String recordId, String pId) throws ErrorMessage{
		this.sAlertTitle=sAlertTitle;
		this.recordId=recordId;
		this.div = new HtmlDiv();
		this.arTexts=new ArrayList<HtmlWindow>();
		this.pId = pId;
	}
	
	private void _create() throws ErrorMessage{
		HtmlWindow text = new Text(sAlertTitle).get();
		int maxLength=TOOLS.getTextLength(sAlertTitle);
		text.setClear(Clear_Type.Both);
		div.put(text);
		
		int curLength;
		for(HtmlWindow ite:this.arTexts){
			ite.setClear(Clear_Type.Both);
			curLength=TOOLS.getPxLength(ite.getLength());
			ite.setStyleWidth(curLength);
			div.put(ite);
			maxLength=curLength>maxLength?curLength:maxLength;
		}
		String width=TOOLS.intToString(maxLength);
		String height=TOOLS.intToString(StyleConst.Single_Row_Height+StyleConst.Per_Row_Height*this.arTexts.size());
		div.setStyleSize(width,height);
	}
	
	/**
	 * 提交动作绑定
	 * @param sActId 提交动作ID
	 * @param sParentCid 父亲容器ID
	 * @throws ErrorMessage 
	 */
	public void bind(String sActId) throws ErrorMessage{
		this.div.bind(SysName.PromptSubmitID,Window_Event.OnClick,
				AjxScript.getSpNoRefresh(SysName.PromoptContentID, sActId,recordId,pId)
				);
		this.div.bind(SysName.PromptSubmitID,Window_Event.OnClick,PopupScript.cancel(SysName.PromptID));
	}
	
	public void put(HtmlWindow text){
		this.arTexts.add(text);
	}
	
	/**
	 * 放入提交附加参数
	 * @param paramName    参数名
	 * @param paramValue   参数值
	 * @throws ErrorMessage
	 */
	public void putAddParams(String paramName,String paramValue) throws ErrorMessage{
		this.div.put(new HiddenParam(paramName,paramValue).get());
	}
	
	/**
	 * 获取提示界面
	 * @return 提示界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get() throws ErrorMessage{
		this._create();
		return this.div;
	}
	
}
